package com.bitdf.txing.group.feign.impl.chat;

import com.bitdf.txing.chat.feign.ChatGroupFeignService;
import com.bitdf.txing.chat.to.GroupRequestAddTo;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.group.entity.GroupRequestAddEntity;
import com.bitdf.txing.group.service.GroupMemberService;
import com.bitdf.txing.group.service.GroupRequestAddService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Lizhiwei
 * @date 2023/9/6 20:37:42
 * 注释：
 */
@Service
@ConditionalOnProperty(value = "tx.deploy.mode", havingValue = "standalone")
public class ChatGroupFeignServiceImpl implements ChatGroupFeignService {

    @Autowired
    GroupMemberService groupMemberService;
    @Autowired
    GroupRequestAddService groupRequestAddService;

    @Override
    public R getMemberIdsByGroupId(Long groupId) {
        List<Long> collect = groupMemberService.getMemberIdsByGroupId(groupId);
        return R.ok().put("data", collect);
    }

    @Override
    public R getGroupAndUserIdsByUserId(Long userId) {
        Map<Long, List<Long>> listMap = groupMemberService.getGroupAndUserIdsByUserId(userId);
        return R.ok().put("data", listMap);
    }

    @Override
    public R save(GroupRequestAddTo groupRequestAdd) {
        GroupRequestAddEntity groupRequestAddEntity = new GroupRequestAddEntity();
        BeanUtils.copyProperties(groupRequestAdd, groupRequestAddEntity);
        boolean save = groupRequestAddService.save(groupRequestAddEntity);
        return R.ok().put("data",groupRequestAdd);
    }

    @Override
    public R agreeGroupRequest(Long id) {
        Boolean b = groupRequestAddService.agree(id);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
    }

    @Override
    public R rejectGroupRequest(Long id) {
        Boolean b = groupRequestAddService.reject(id);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
    }

    @Override
    public R addGroupMember(Long groupId, Long userId) {
        boolean b = groupMemberService.addMember(groupId, userId);
        return R.ok();
    }
}
